// pages/transmit/transmit.js
var that;
const app = getApp()
Page({

  /**
   * 
   */
  data: {
    playing:false,
    musStatus:{},
    songState:{
      progress: 0,
      currentPosition: '00:00',
      duration: '00:00',
      downloadPercent:0
    },
    musList:[
      {
        voiceid:0,
        url: 'http://cdnergebd.shoujiduoduo.com//user_audio//75//user_1359240_1522148896275.mp3',
        logo:'../../images/imagesStoryList/playList/play0.png',
        title_name:'小猪佩奇',
        list_name:'芒果妈妈睡觉故事01'
      },
      {
        voiceid:1,
        url: 'http:\/\/cdnergebd.shoujiduoduo.com\/user_audio\/56\/user_2157830_1523707996056.mp3',
        logo:'../../images/imagesStoryList/playList/play1.png',
        title_name:'茜茜妈妈',
        list_name:'芒果妈妈睡觉故事01'
      },
      {
        voiceid:2,
        url: 'http:\/\/cdnergebd.shoujiduoduo.com\/user_audio\/73\/user_1359240_1520431127073_vn.mp3',
        logo:'../../images/imagesStoryList/playList/play2.png',
        title_name:'橘子老虎',
        list_name:'芒果妈妈睡觉故事01'
      }
    ],
  },

  // 转换时间格式
  timeToString: function (duration) {
    var str = '';
    var minute = parseInt(duration / 60) < 10 ? ('0' + parseInt(duration / 60)) : (parseInt(duration / 60));
    var second = duration % 60 < 10 ? ('0' + duration % 60) : (duration % 60);
    str = minute + ':' + second;
    return str;
  },


   //播放音乐
  songPlay: function () {
    clearInterval(timer);
    var timer = setInterval(function () {
      // 获取后台音乐播放状态
      wx.getBackgroundAudioPlayerState({
        success: function (res) {
          // 播放状态 1表示播放中
          // console.log(res)
          if (res.status == 1) {
            that.setData({
              playing: true,
              songState: {
                progress: parseInt( res.currentPosition / res.duration * 100 ) + '%',
                currentPosition: that.timeToString(res.currentPosition),
                duration: that.timeToString(res.duration),
                downloadPercent:res.downloadPercent
              }
            });
          } else{
            that.setData({playing: false});
            clearInterval(timer);
          }
        }
      });
    }, 1000);
  },

  //上一首 <=> 下一首 
  playother:function(event){
    that.songPlay()
    let upOrDown = event.currentTarget.dataset.other//上首歌，下首歌 按钮切换标识 {-1:上首歌 , 1:下首歌}
    let voiceid = this.data.voiceid//当前音乐id 及列表中的位置
    let mucLength = this.data.musList.length//音乐列表长度
    if(upOrDown == -1){
      voiceid -= 1
      if(voiceid < 0){
        voiceid = this.data.musList.length-1
      }
    }else if(upOrDown == 1){
      voiceid += 1
      if(voiceid == mucLength){
        voiceid = 0
      }
    }
    this.setData({
      'musStatus':this.data.musList[voiceid],
      'voiceid':voiceid
    })
    wx.playBackgroundAudio({
        dataUrl: this.data.musStatus.url,
        title: this.data.musStatus.title_name,
        coverImgUrl: this.data.musStatus.logo
    })
    that.changeNav()
  },

  //播放 <=> 暂停  
  playingtoggle:function(event){
    let playstatus = event.currentTarget.dataset.p//得到传值这个播放状态。
    if(playstatus){
      wx.pauseBackgroundAudio()// 暂停
      app.playControl(this)
    }else{
      // that.songPlay()
      wx.playBackgroundAudio()// 续播
      app.playControl(this)
    }
  },


  onLoad:function(options){
    console.log(options)
    that = this
    //当前播放音乐数据插入
    this.setData({
      'musStatus':this.data.musList[2],
      'voiceid':2
    })
    that.changeNav()
    wx.playBackgroundAudio({
        dataUrl: this.data.musStatus.url,
        title: this.data.musStatus.title_name,
        coverImgUrl: this.data.musStatus.logo
    })//进入页面就开始播放
  },
  //修改标题
  changeNav:function(){
    let title = this.data.musStatus.title_name;
    wx.setNavigationBarTitle({
      title: title
    })
  },
  onReady:function(){
    let that = this

    that.songPlay();



    // wx.seekBackgroundAudio({
    //     position: 100
    // })


  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
              // 来自页面内转发按钮
    };
    return {
      title:'童话故事',
      path: '/pages/index/index',
      success: function(res) {
        // 转发成功
        console.log("shareAppMessage: ok")
        console.log(res);
      },
      fail: function(res) {
        // 转发失败
        console.log(res);
      }
    }
  }
})